package com.cola.colaboot.config.security;

import com.alibaba.fastjson.JSONObject;
import com.cola.colaboot.config.dto.Res;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class NoAccessHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException {
        Res<?> fail = Res.fail("权限校验失败！");
        fail.setCode(301);
        response.setStatus(200);
        PrintWriter writer = response.getWriter();
        writer.write(JSONObject.toJSONString(fail));
        writer.close();
    }
}
